package com.kaigejava.webmaginc.csdn;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

/**
 * @author kaigejava
 * csdn文章详情解析
 */
public class CsdnArticleInfoPage implements PageProcessor {
    public void process(Page page) {
        this.getInfo(page);
    }

    private void getInfo(Page page) {

        Html html = page.getHtml();
        String tile = html.css("div.article-title-box h1","text").toString();
        //page.getHtml().xpath(titleXpath).get().toString();
        String articleTypeStr = html.css("div.article-bar-top span","text").toString();
        //page.getHtml().xpath(articleTypeXpath).toString();
        String nickName = html.css("div.article-bar-top a","text").toString();
        //page.getHtml().xpath(nickNameXpath).toString();
        String time = html.css("div.article-bar-top span.time","text").toString().replace("最后发布于","");
        //page.getHtml().xpath(timeXpath).toString().replace("最后发布于","");
        String readCount = html.css("div.article-bar-top span.read-count","text").toString().replace("阅读数 ","");
        //page.getHtml().xpath(readCountXpath).toString().replace("阅读数 ","");

        String content = html.xpath("//*[@id=\"content_views\"]").toString();
                //html.css("div.clearfix","text").toString();
        System.out.println("tile:"+tile+"-readCount："+readCount+" ==content:"+content);
    }

    public Site getSite() {
        return Site.me().setSleepTime(100).setRetryTimes(3);
    }
    public static void main(String[] args) {
        Spider spider = Spider.create(new CsdnArticleInfoPage()).addUrl("https://blog.csdn.net/ThinkWon/article/details/103592572");
        spider.addRequest().run();
    }
}
